package middleware

import (
	"XiangMu/utils"
	"github.com/gin-gonic/gin"
)

func AuthDangWeiCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		userClaim, err := c.Get("user")
		if !err {
			c.Abort()
			c.JSON(200, gin.H{
				"code": 401,
				"msg":  "登录错误",
			})
			return
		}
		if user, ok := userClaim.(*utils.UserClaims); ok {
			if userClaim == nil || user.Auth != 2 {
				c.Abort()
				c.JSON(200, gin.H{
					"code": 401,
					"msg":  "不是党委",
				})
				return
			}
			c.Next()
		} else {
			c.Abort()
			// 如果断言失败，返回错误
			c.JSON(500, gin.H{"message": "Failed to assert User"})
			return
		}

	}
}
